package com.jackrain.nea.omslocal.mq;

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.omslocal.Dao.JingDongRefundDao;
import com.jackrain.nea.omslocal.service.SendMqServies;
import com.jackrain.nea.util.ApplicationContextHandle;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.aliyun.openservices.ons.api.Action.CommitMessage;
import static com.aliyun.openservices.ons.api.Action.ReconsumeLater;

/**
 * @Discription: 京东退单下载监听
 * Created with IntelliJ IDEA.
 * @Author wcy
 * @Date 2018/5/9 9:12
 */
@Slf4j
public class JingDongRefundListener implements MessageListener{

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        System.out.println("消费消息===========");
        SendMqServies sendMq = ApplicationContextHandle.getBean(SendMqServies.class);
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        try {
            //  京东退单下载消息 topic
            if (propertiesConf.getProperty("ons.jdRefundTopic").equals(message.getTopic())) {
                // 京东退单下载消息消费
                mqJingDongRefundConsume(message, consumeContext);
            } else {
                // 不是本监听的消息
                log.error(" topic 配置错误, 消费到不是本监听的消息");
            }
            return CommitMessage;

        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

            // 逐条将错误信息以消息发送到线上发送到线上
            JSONObject errObj = new JSONObject();
            errObj.put("flag",0);
            errObj.put("topic", message.getTopic());
            errObj.put("tag", message.getTag());
            errObj.put("msgkey", message.getKey());
            // 线下消费报错的主机 ip
            errObj.put("ip", NetUtils.getLocalAddress());
            // 将错误以流写出
            e.printStackTrace(pw);
            errObj.put("errmsg", sw.toString());
            errObj.put("sendtime", new Date());
            try {
                sendMq.sendMqObj(errObj, propertiesConf.getProperty("ons.jdLogErrTopic"), message.getTag());
            } catch (Exception e1) {
                e1.printStackTrace();
                log.debug("京东退单发送错误消息失败");
            }
            e.printStackTrace();
            return ReconsumeLater;
        }
    }

    private void mqJingDongRefundConsume(Message message, ConsumeContext consumeContext) throws Exception{
        JingDongRefundDao dao = ApplicationContextHandle.getBean(JingDongRefundDao.class);
        JSONArray tempArr = JSONObject.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
        dao.refundStart(tempArr,message.getKey(),message.getTopic(),message.getTag());
        if (tempArr == null) {
            throw new NDSException("京东退单下载回执数据为空");
        }
    }
}
